package demo01;

import java.util.Objects;

public class Demo03Objects {
    public static void main(String[] args) {
        String s1 = "abc";
//        String s1=null;// java.lang.NullPointerException空指针异常
        String s2 = "abc";
        String s3 = null;
        boolean equals = s1.equals(s2);//相同地址，因为字符串一样，节约空间
       /*
            Objects类的equals方法:对两个对象进行比较,防止空指针异常
            public static boolean equals(Object a, Object b) {
                return (a == b) || (a != null && a.equals(b));
            }
         */

        boolean equals1 = Objects.equals(s3, s2);

        System.out.println(equals);
        System.out.println(equals1);//不会在字符串为空时报错

    }
}
